
package pe.com.yugi.DAO.Impl;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import pe.com.yugi.DAO.DetalleCombDAO;
import pe.com.yugi.Dominio.Carta;
import pe.com.yugi.Dominio.DetalleComb;
import pe.com.yugi.Util.HibernateUtil;


public class DetalleCombDAOImpl implements DetalleCombDAO, Serializable{

    @Override
    public boolean agregar(DetalleComb c) {
        boolean agreg = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(c);
            session.getTransaction().commit();
            agreg = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return agreg;
    }

    @Override
    public List<DetalleComb> listarDetalle(int idCarta) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from DetalleComb where idCarta="+idCarta);
        List<DetalleComb> list = query.list();
        for(DetalleComb c : list){
            Hibernate.initialize(c.getCarta());
            Hibernate.initialize(c.getCombinacion());
        }
        return list;
    }

    @Override
    public List<DetalleComb> listarDetalleComb(int idCombinacion) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from DetalleComb where combinacion.idCombinacion="+idCombinacion);
        List<DetalleComb> list = query.list();
        for(DetalleComb c : list){
            Hibernate.initialize(c.getCarta());
            Hibernate.initialize(c.getCombinacion());
            System.out.println("Carta:"+c.getCarta().getNombre());
        }
        return list;
    }
}
